package com.zyh.actionitembadge_ohos.slice;

import com.mikepenz.actionitembadge.library.ActionItemBadge;

import com.mikepenz.actionitembadge.library.ActionItemBadgeAdder;
import com.mikepenz.actionitembadge.library.utils.BadgeStyle;

import com.mikepenz.actionitembadge.library.utils.NumberUtils;
import com.mikepenz.actionitembadge.library.utils.RecordStatus;
import com.mikepenz.actionitembadge.library.utils.ToastsUtils;
import com.mikepenz.iconics.IconicsDrawable;
import com.mikepenz.iconics.typeface.library.fontawesome.FontAwesome;
import com.mikepenz.iconics.typeface.library.fontawesome.Initializer;

import com.mikepenz.iconics.view.IconicsImageView;
import com.zyh.actionitembadge_ohos.PopupWindowDialog;
import com.zyh.actionitembadge_ohos.ResourceTable;
import com.zyh.actionitembadge_ohos.SlidingDrawerTest;
import com.zyh.actionitembadge_ohos.other.LeftBean;
import com.zyh.actionitembadge_ohos.other.ListAdapter;
import com.zyh.actionitembadge_ohos.other.ListBeanContent;
import com.zyh.actionitembadge_ohos.other.ListLeftAdapter;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;

import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;

import ohos.agp.components.Text;

import ohos.agp.text.RichText;
import ohos.agp.text.RichTextBuilder;
import ohos.agp.text.TextForm;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;

import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import hollowsoft.slidingdrawer.OnDrawerCloseListener;
import hollowsoft.slidingdrawer.OnDrawerOpenListener;

/***
 *  MainAbilitySlice
 *
 **/
public class MainAbilitySlice extends AbilitySlice implements ListLeftAdapter.ItemOnClickListener,
    ActionItemBadge.ActionItemBadgeListener, ActionItemBadge.ActionItemBadgeLongListener {
    private static HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x000110, "ASICON");
    private static HiLogLabel label1 = new HiLogLabel(HiLog.LOG_APP, 0x000110, "ASICON_100M");
    private final String NEWLINE = System.getProperty("line.separator");
    private BadgeStyle nstyle = ActionItemBadge.BadgeStyles.BLUE.getStyle();
    private BadgeStyle bigStyle = ActionItemBadge.BadgeStyles.DARK_GREY_LARGE.getStyle();
    private int badgeCount = 10;
    private int badgeDrawableCount = 100000000;

    private RichTextBuilder richTextBuilder;
    private int recordbadgeDrawableCount = -20000000;

    private PopupWindowDialog popupWindowDialog;
    private Component componentline;
    private EventHandler mEventHandler;
    private Runnable task, task1;
    public static final int DELAY_MSG_TIME = 2000;

    private boolean step1 = false; // 点击switch
    private boolean step2 = false; // 点击hide按钮

    private SlidingDrawerTest slidingDrawer;
    private Component handle;
    private DirectionalLayout toolbar_direct;
    private Text text_showleft;
    private Component component,
        component_bg;
    private DirectionalLayout dl_bottomtitle,
        dl_toptitle,
        toolbar_dependentLayout;
    private ListContainer list_main,
        listContainer_right;

    private List<ListBeanContent> beanContentList;
    private List<LeftBean> leftList;
    private Text ability_main_Activity,
        ability_main_SWITCH,
        ability_main_RESET,
        ability_main_HIDE,
        ActionItemBadge_Id;
    Component component01;
    private int currentPos = -2000;
    private boolean isNormal = true;
    private int beforeP = -2000;   // 统计前一个的位置
    private int beforeM = -2000;  // 统计前一个图标数据

    private boolean isCheckActivity = false;  // 是否切换了Activity
    private boolean isResetCount = false; // 是否reset
    private Component component02, component03, component04, component05;
    private IconicsDrawable robot_drawable;
    private ToastsUtils toastsUtils;

    private ToastsUtils getToastsUtils(String text) {
        if (toastsUtils == null) {
            toastsUtils = new ToastsUtils.Builder(MainAbilitySlice.this)
                .setToastText("No icon provided for the previous large style").setDuration(3000).build();
        }
        return toastsUtils;
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        getWindow().setStatusBarColor(Color.getIntColor("#f34050B5"));
        richTextBuilder = new RichTextBuilder();
        initDate();
        mEventHandler = new EventHandler(EventRunner.getMainEventRunner());
        toastsUtils = getToastsUtils("No icon provided for the previous large style");
        componentline = (Component) findComponentById(ResourceTable.Id_componentline);
        ActionItemBadge_Id = (Text) findComponentById(ResourceTable.Id_ActionItemBadge_Id);
        dl_toptitle = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_toptitle);
        dl_bottomtitle = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_bottomtitle);
        toolbar_dependentLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_toolbar_dependentLayout);
        handle = (Component) findComponentById(ResourceTable.Id_handle);
        slidingDrawer = (SlidingDrawerTest) findComponentById(ResourceTable.Id_my_slidingdrawer);
        text_showleft = (Text) findComponentById(ResourceTable.Id_text_showleft);
        component_bg = (Component) findComponentById(ResourceTable.Id_component_bg);
        list_main = (ListContainer) findComponentById(ResourceTable.Id_list_main);
        listContainer_right = (ListContainer) findComponentById(ResourceTable.Id_list);
        ability_main_Activity = (Text) findComponentById(ResourceTable.Id_ability_main_Activity);
        ability_main_SWITCH = (Text) findComponentById(ResourceTable.Id_ability_main_SWITCH);
        ability_main_RESET = (Text) findComponentById(ResourceTable.Id_ability_main_RESET);
        ability_main_HIDE = (Text) findComponentById(ResourceTable.Id_ability_main_HIDE);
        left();
        ability_main_Activity.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                slidingDrawer.animateClose();
                beforeM = badgeCount;
                beforeP = currentPos;
                recordbadgeDrawableCount = badgeDrawableCount;
                badgeDrawableCount = 0;
                badgeCount = 10;
                handle.setWidth(0);
                ActionItemBadge_Id.setVisibility(Component.INVISIBLE);
                dl_toptitle.setVisibility(Component.INVISIBLE);
                isCheckActivity = true;
                changePosition(1);
            }
        });

        ability_main_SWITCH.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                step1 = true;
                beforeP = currentPos;
                slidingDrawer.animateClose();
                isCheckActivity = false;
                isNormal = !isNormal;
                HiLog.info(label, "SWITCH hahhaha " + currentPos + " isNormal " + isNormal);
                changePosition(currentPos);
                new ToastsUtils.Builder(component.getContext()).setToastText(
                    "No icon provided for the previous large style").setDuration(3000).build().show();
            }
        });

        ability_main_RESET.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                int badgeCountY = badgeCount;
                step1 = false;
                step2 = false;
                badgeCount = 10;
                isResetCount = true;
                slidingDrawer.animateClose();
                changePosition(currentPos);
            }
        });

        ability_main_HIDE.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                badgeCount = Integer.MIN_VALUE;
                slidingDrawer.animateClose();
                if (step1 = true) {
                    step2 = true;
                }
                changePosition(currentPos);
            }
        });

        component_bg.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                slidingDrawer.animateClose();
            }
        });

        text_showleft.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                slidingDrawer.animateOpen();
            }
        });

        slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
            @Override
            public void onDrawerOpened() {
                component_bg.setVisibility(Component.VISIBLE);
            }
        });

        slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
            @Override
            public void onDrawerClosed() {
                component_bg.setVisibility(Component.HIDE);
            }
        });

        handle.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                slidingDrawer.animateClose();
            }
        });

        changePosition(2);
        adapteList();
    }

    private void left(){
        TextForm textForm = new TextForm();
        textForm.setTextSize(AttrHelper.vp2px(15, MainAbilitySlice.this));
        richTextBuilder.mergeForm(textForm);

        richTextBuilder.addText(ability_main_Activity.getText());
        richTextBuilder.revertForm();
        RichText richText = richTextBuilder.build();
        ability_main_Activity.setRichText(richText);

        textForm = new TextForm();
        textForm.setTextSize(AttrHelper.vp2px(15, MainAbilitySlice.this));
        richTextBuilder.mergeForm(textForm);
        richTextBuilder.addText(ability_main_SWITCH.getText());
        richTextBuilder.revertForm();
        RichText richText1 = richTextBuilder.build();
        ability_main_SWITCH.setRichText(richText1);

        textForm = new TextForm();
        textForm.setTextSize(AttrHelper.vp2px(15, MainAbilitySlice.this));
        richTextBuilder.mergeForm(textForm);
        richTextBuilder.addText(ability_main_RESET.getText());
        richTextBuilder.revertForm();
        RichText richText2 = richTextBuilder.build();
        ability_main_RESET.setRichText(richText2);

        textForm = new TextForm();
        textForm.setTextSize(AttrHelper.vp2px(15, MainAbilitySlice.this));
        richTextBuilder.mergeForm(textForm);
        richTextBuilder.addText(ability_main_HIDE.getText());
        richTextBuilder.revertForm();
        RichText richText3 = richTextBuilder.build();
        ability_main_HIDE.setRichText(richText3);
    }

    @Override
    public boolean onOptionsItemSelected(Component component) {
        int id = component.getId();

        if (id == ResourceTable.Id_item_samplebadge) {

            new ToastsUtils.Builder(component.getContext()).setToastText("Sample 3").setDuration(2000).build().show();
              badgeCount--;
            if (component02 == null) {
                component02 = component01.findComponentById(ResourceTable.Id_item_samplebadge);
            }
            if (isCheckActivity == true) {
                if (badgeCount > 0) {
                    ActionItemBadge.update(component02, badgeCount);
                } else {
                    ActionItemBadge.hide(component02);
                }
            } else {
                    if (badgeCount == Integer.MIN_VALUE || badgeCount == Integer.MAX_VALUE) {
                    step2 = false;
                    if (isResetCount == true) {
                        isResetCount = false;
                    }
                    changeLayout();
                }
                Text cbadge = (Text) component02.findComponentById(ResourceTable.Id_menu_badge);
                cbadge.setText("" + badgeCount);
                ActionItemBadge.update(component02, badgeCount);
            }

        } else if (id == ResourceTable.Id_item_sampleBadge_drawable) {
            new ToastsUtils.Builder(component.getContext()).setToastText("Sample 3").setDuration(2000).build().show();
             badgeDrawableCount = badgeDrawableCount - 1000;
            if (component03 == null) {
                component03 = component01.findComponentById(ResourceTable.Id_item_sampleBadge_drawable);
            }
            ActionItemBadge.update(component03,
                NumberUtils.formatNumber(badgeDrawableCount));
        } else if (id == ResourceTable.Id_item_sampleBadge_large) {
            new ToastsUtils.Builder(component.getContext()).setToastText("Sample 3").setDuration(2000).build().show();
            badgeCount--;
            if (component04 == null) {
                component04 = component01.findComponentById(ResourceTable.Id_item_sampleBadge_large);
            }
            ActionItemBadge.update(component04, badgeCount);
        } else if (id == ResourceTable.Id_item_sampleBadge_large2) {
            new ToastsUtils.Builder(component.getContext()).setToastText("Sample 3").setDuration(2000).build().show();
            badgeDrawableCount = badgeDrawableCount - 1000;
            if (component05 == null) {
                component05 = component01.findComponentById(ResourceTable.Id_item_sampleBadge_large2);
            }
            DirectionalLayout.LayoutConfig layoutConfig0 = new DirectionalLayout.LayoutConfig();
            layoutConfig0.width = AttrHelper.vp2px(50, MainAbilitySlice.this);
            ActionItemBadge.update(component05,
                NumberUtils.formatNumber(badgeDrawableCount));
        } else {
            new ToastsUtils.Builder(component.getContext()).setToastText("Sample 4").setDuration(3000).build().show();
        }
        return false;
    }

    /***
     * 设置BadgeStyle变化指令
     * @param position
     ***/
    public void changePosition(int position) {
        currentPos = position;
        if (position == 0) {
            nstyle = ActionItemBadge.BadgeStyles.DARK_GREY.getStyle();
            bigStyle = ActionItemBadge.BadgeStyles.DARK_GREY_LARGE.getStyle();
        } else if (position == 1) {
            nstyle = ActionItemBadge.BadgeStyles.GREY.getStyle();
            bigStyle = ActionItemBadge.BadgeStyles.GREY_LARGE.getStyle();
        } else if (position == 2) {
            nstyle = ActionItemBadge.BadgeStyles.RED.getStyle();
            bigStyle = ActionItemBadge.BadgeStyles.RED_LARGE.getStyle();
        } else if (position == 3) {
            nstyle = ActionItemBadge.BadgeStyles.BLUE.getStyle();
            bigStyle = ActionItemBadge.BadgeStyles.BLUE_LARGE.getStyle();
        } else if (position == 4) {
            nstyle = ActionItemBadge.BadgeStyles.GREEN.getStyle();
            bigStyle = ActionItemBadge.BadgeStyles.GREEN_LARGE.getStyle();
        } else if (position == 5) {
            nstyle = ActionItemBadge.BadgeStyles.PURPLE.getStyle();
            bigStyle = ActionItemBadge.BadgeStyles.PURPLE_LARGE.getStyle();
        } else if (position == 6) {
            nstyle = ActionItemBadge.BadgeStyles.YELLOW.getStyle();
            bigStyle = ActionItemBadge.BadgeStyles.YELLOW_LARGE.getStyle();
        } else if (position == 7) {
            nstyle = new BadgeStyle(BadgeStyle.Style.DEFAULT, ResourceTable.Layout_menu_action_item_badge, Color.getIntColor("#FE0665"), Color.getIntColor("#CC0548"), Color.getIntColor("#EEEEEE"));
            bigStyle = new BadgeStyle(BadgeStyle.Style.LARGE, ResourceTable.Layout_menu_action_item_badge_large, Color.getIntColor("#FE0665"), Color.getIntColor("#CC0548"), Color.getIntColor("#EEEEEE"));
        }
        nstyle.setCorner(20);
        bigStyle.setCorner(20);
        if (isNormal == false) {
            BadgeStyle temp = nstyle;
            nstyle = bigStyle;
            bigStyle = temp;
        }
        toolbar_dependentLayout.setOrientation(Component.HORIZONTAL);
        HiLog.info(label1, "isCheckActivity : " + isCheckActivity + ", isCheckHide : " + isCheckHide
            + ", isSwitch: " + isNormal + ", badcount: " + badgeCount);
        if (isCheckActivity && isCheckHide) {
            if (toolbar_dependentLayout.getChildCount() > 0) {
                Component[] components = new Component[toolbar_dependentLayout.getChildCount()];
                for (int i = 0; i < toolbar_dependentLayout.getChildCount(); i++) {
                    components[i] = toolbar_dependentLayout.getComponentAt(i);
                }
                DirectionalLayout.LayoutConfig layoutConfig1 = new DirectionalLayout.LayoutConfig();
                DirectionalLayout.LayoutConfig layoutConfig2 = new DirectionalLayout.LayoutConfig();
                DirectionalLayout.LayoutConfig layoutConfig3 = new DirectionalLayout.LayoutConfig();
                DirectionalLayout.LayoutConfig layoutConfig4 = new DirectionalLayout.LayoutConfig();
                layoutConfig1.weight = 14;
                layoutConfig2.weight = 5;
                layoutConfig3.weight = 3;
                layoutConfig4.weight = 2;
                components[0].setLayoutConfig(layoutConfig1);
                components[1].setLayoutConfig(layoutConfig2);
                components[0].setLeft(AttrHelper.vp2px(50, MainAbilitySlice.this));

                HiLog.info(label, "H_A " + components[0].getName() + " GG " + components[1].getName());
                ComponentContainer componentContainers = (ComponentContainer) components[1];
                Component component0001A, component0001B;
                component0001A = componentContainers.getComponentAt(0);// 第一个布局
                HiLog.info(label, "H_Akk " + component0001A.getName());
                component0001B = component0001A.findComponentById(ResourceTable.Id_menu_badge);
                component0001B.setLeft(AttrHelper.vp2px(6, MainAbilitySlice.this));

            }
        }
        // 如果出现问题, 优先删除
        if (isResetCount && isCheckHide) {
            if (toolbar_dependentLayout.getChildCount() > 0) {
                Component[] components = new Component[toolbar_dependentLayout.getChildCount()];
                for (int i = 0; i < toolbar_dependentLayout.getChildCount(); i++) {
                    components[i] = toolbar_dependentLayout.getComponentAt(i);
                }
                DirectionalLayout.LayoutConfig layoutConfig1 = new DirectionalLayout.LayoutConfig();
                DirectionalLayout.LayoutConfig layoutConfig2 = new DirectionalLayout.LayoutConfig();
                DirectionalLayout.LayoutConfig layoutConfig3 = new DirectionalLayout.LayoutConfig();
                DirectionalLayout.LayoutConfig layoutConfig4 = new DirectionalLayout.LayoutConfig();
                layoutConfig1.weight = 10;
                layoutConfig2.weight = 7;
                layoutConfig3.weight = 3;
                layoutConfig4.weight = 2;
                layoutConfig1.setMarginLeft(AttrHelper.vp2px(80, MainAbilitySlice.this));
                components[0].setLayoutConfig(layoutConfig1);
                components[1].setLayoutConfig(layoutConfig2);
                ActionItemBadge_Id = (Text) components[0];
            }
        }

        if (component01 == null) {
            component01 = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_main, null, false);
            DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig();
            toolbar_dependentLayout.setAlignment(LayoutAlignment.VERTICAL_CENTER);
            layoutConfig.setMarginLeft(20);
            /*   layoutConfig.weight = 4;*/
            layoutConfig.alignment = LayoutAlignment.VERTICAL_CENTER;
            layoutConfig.height = AttrHelper.vp2px(50, getContext());
            component01.setLayoutConfig(layoutConfig);
            DirectionalLayout.LayoutConfig config002 = new DirectionalLayout.LayoutConfig();
            /*  config002.weight = 6 ;*/
            config002.setMarginLeft(AttrHelper.vp2px(80, MainAbilitySlice.this));
            toolbar_dependentLayout.addComponent(component01);
            ActionItemBadge_Id.setLayoutConfig(config002);
        }
        ActionItemBadge.setMyListener(this);
        ActionItemBadge.setLongClickListener(this);
        Component c01 = component01.findComponentById(ResourceTable.Id_item_sampleBadge_large);
        Component c02 = component01.findComponentById(ResourceTable.Id_item_sampleBadge_large2);
        Text menubadge = (Text) c01.findComponentById(ResourceTable.Id_menu_badge);
        Text menubadge2 = (Text) c02.findComponentById(ResourceTable.Id_menu_badge);
        if (badgeCount == Integer.MAX_VALUE || badgeCount == Integer.MIN_VALUE || badgeCount > Integer.MAX_VALUE - 100000000 || badgeCount < Integer.MIN_VALUE + 100000000) {
            int size11 = AttrHelper.fp2px(16, MainAbilitySlice.this);
            int size16 = AttrHelper.fp2px(16, MainAbilitySlice.this);
            if (isCheckHide == true) {
                menubadge.setTextSize(size11);
                menubadge2.setTextSize(size11);
            } else {
                menubadge.setTextSize(size16);
                menubadge2.setTextSize(size16);
            }

        } else {
            int size16 = AttrHelper.fp2px(16, MainAbilitySlice.this);
            menubadge.setTextSize(size16);
            menubadge2.setTextSize(size16);
        }

        if (badgeCount == 0) {
            ActionItemBadge.hide(component01.findComponentById(ResourceTable.Id_item_samplebadge));
        } else {
            HiLog.info(label, " " + "itemOnClickListener4  " + position + " zc " + isNormal);
            AbilitySlice slice = MainAbilitySlice.this;
            Initializer.init(slice.getContext());
            HiLog.info(label, " badgeCount " + badgeCount);
            ActionItemBadge.setMyListener(this);
            ActionItemBadge.setIsHideTextbg(false);
            if (isNormal == true) {
                HiLog.info(label, " itemOnClickListener4 " + isNormal + "  ");
                component01.findComponentById(ResourceTable.Id_item_samplebadge).setVisibility(Component.VISIBLE);
                Component component007 = component01.findComponentById(ResourceTable.Id_item_samplebadge);
                component01.findComponentById(ResourceTable.Id_item_sampleBadge_large).setVisibility(Component.HIDE);
                Component component008 = component007.findComponentById(ResourceTable.Id_menu_badge);
                DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig();
                ActionItemBadge.setIsCheck(true);
                component008.setLayoutConfig(layoutConfig);
                Component imageTes = findComponentById(ResourceTable.Id_menu_badge_icon);
                robot_drawable = new IconicsDrawable(imageTes, component01.getContext(), FontAwesome.Icon.faw_cloud_sun);
                ActionItemBadge.update(slice, component01.findComponentById(ResourceTable.Id_item_samplebadge), FontAwesome.Icon.faw_cloud_sun, nstyle, badgeCount);

            } else {
                ActionItemBadge.setIsHideTextbg(true);
                if (isCheckActivity == false) {
                    component01.findComponentById(ResourceTable.Id_item_samplebadge).setVisibility(Component.HIDE);
                    component01.findComponentById(ResourceTable.Id_item_sampleBadge_large).setVisibility(Component.VISIBLE);

                    if (step2 == true) {
                        ActionItemBadge.hide(component01.findComponentById(ResourceTable.Id_item_sampleBadge_large));
                    } else {
                        HiLog.info(label1, "MAS_setCheck ");
                        ActionItemBadge.setIsCheck(false);
                        ActionItemBadge.update(MainAbilitySlice.this, component01.findComponentById(ResourceTable.Id_item_sampleBadge_large), null, nstyle, badgeCount, null);
                    }
                } else {
                    ActionItemBadge.setIsCheck(true);
                    component01.findComponentById(ResourceTable.Id_item_samplebadge).setVisibility(Component.VISIBLE);
                    component01.findComponentById(ResourceTable.Id_item_sampleBadge_large).setVisibility(Component.HIDE);
                    ActionItemBadge.update(slice, component01.findComponentById(ResourceTable.Id_item_samplebadge), FontAwesome.Icon.faw_cloud_sun, nstyle, badgeCount);
                }
            }
        }

        if (badgeDrawableCount == 0) {
            if (isNormal == true) {
                ActionItemBadge.setIsHideTextbg(true);
                ActionItemBadge.setIsCheck(true);
                ActionItemBadge.hide(component01.findComponentById(ResourceTable.Id_item_sampleBadge_drawable));
            } else {
                ActionItemBadge.setIsHideTextbg(true);
                ActionItemBadge.setIsCheck(true);
                ActionItemBadge.hide(component01.findComponentById(ResourceTable.Id_item_sampleBadge_large2));
            }

        } else {
            ActionItemBadge.setMyListener(this);
            ActionItemBadge.setIsHideTextbg(false);
            HiLog.info(label, " " + NumberUtils.formatNumber(badgeDrawableCount));
            if (isNormal == true) {
                ActionItemBadge.setIsHideTextbg(false);
                ActionItemBadge.setIsCheck(true);
                component01.findComponentById(ResourceTable.Id_item_sampleBadge_drawable).setVisibility(Component.VISIBLE);
                component01.findComponentById(ResourceTable.Id_item_sampleBadge_large2).setVisibility(Component.HIDE);
                ActionItemBadge.update(MainAbilitySlice.this, component01.findComponentById(ResourceTable.Id_item_sampleBadge_drawable), ResourceTable.Media_ic_notification, nstyle, NumberUtils.formatNumber(badgeDrawableCount));
            } else {
                ActionItemBadge.setIsHideTextbg(true);
                component01.findComponentById(ResourceTable.Id_item_sampleBadge_drawable).setVisibility(Component.HIDE);
                component01.findComponentById(ResourceTable.Id_item_sampleBadge_large2).setVisibility(Component.VISIBLE);
                DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig();
                layoutConfig.width = AttrHelper.vp2px(20, MainAbilitySlice.this);
                layoutConfig.height = AttrHelper.vp2px(20, MainAbilitySlice.this);

                Component component06 = component01.findComponentById(ResourceTable.Id_menu_badge);
                component06.setLayoutConfig(layoutConfig);
                ActionItemBadge.setIsCheck(true);
                ActionItemBadge.update(MainAbilitySlice.this, component01.findComponentById(ResourceTable.Id_item_sampleBadge_large2), Integer.MAX_VALUE, nstyle, NumberUtils.formatNumber(badgeDrawableCount));

            }
        }
        ActionItemBadge.setMyListener(this);
        if (isResetCount && isCheckHide) {
            RecordStatus.setHideCheck(true);
        } else {
            RecordStatus.setHideCheck(false);
        }
        if (isNormal == true) {
            ActionItemBadge.setIsHideTextbg(true);
            if (toolbar_dependentLayout.getChildCount() > 2)
                toolbar_dependentLayout.removeComponentAt(toolbar_dependentLayout.getChildCount() - 1);
            RecordStatus.setSwitchAbility(false);
            new ActionItemBadgeAdder().act(MainAbilitySlice.this).menu(toolbar_dependentLayout).title("2").itemDetails(0, 1, 1).showAsAction(2).add(bigStyle, 1);
        } else {
            if (isCheckActivity == true) {
                RecordStatus.setSwitchAbility(false);
                ActionItemBadge.setIsHideTextbg(true);
            } else {
                RecordStatus.setSwitchAbility(true);
                ActionItemBadge.setIsHideTextbg(false);
            }

            toolbar_dependentLayout.removeComponentAt(toolbar_dependentLayout.getChildCount() - 1);
            new ActionItemBadgeAdder().act(MainAbilitySlice.this).menu(toolbar_dependentLayout).title("2")
                .itemDetails(0, 1, 1).showAsAction(2).add(bigStyle, 1);
        }

    }

    private boolean isCheckHide = false;

    private void changeLayout() {
        if (component01 != null) {
            isCheckHide = true;
            Component c05 = toolbar_dependentLayout.getComponentAt(toolbar_dependentLayout.getChildCount() - 1);
            Component c01 = component01.findComponentById(ResourceTable.Id_item_samplebadge);
            Component c02 = component01.findComponentById(ResourceTable.Id_item_sampleBadge_drawable);
            Component c03 = component01.findComponentById(ResourceTable.Id_item_sampleBadge_large);
            Component c04 = component01.findComponentById(ResourceTable.Id_item_sampleBadge_large2);
            toolbar_dependentLayout.removeAllComponents();
            DirectionalLayout.LayoutConfig layoutConfig1 = new DirectionalLayout.LayoutConfig();
            DirectionalLayout.LayoutConfig layoutConfig2 = new DirectionalLayout.LayoutConfig();
            DirectionalLayout.LayoutConfig layoutConfig3 = new DirectionalLayout.LayoutConfig();
            DirectionalLayout.LayoutConfig layoutConfig4 = new DirectionalLayout.LayoutConfig();
            DirectionalLayout.LayoutConfig layoutConfig5 = new DirectionalLayout.LayoutConfig();
            layoutConfig1.weight = 4;
            layoutConfig2.weight = 8;
            layoutConfig3.weight = 3;

            layoutConfig4.weight = 1;
            layoutConfig5.weight = 5;
            layoutConfig1.setMarginLeft(AttrHelper.vp2px(80, MainAbilitySlice.this));
            ActionItemBadge_Id.setLayoutConfig(layoutConfig1);
            layoutConfig2.width = AttrHelper.vp2px(180, MainAbilitySlice.this);
            c01.findComponentById(ResourceTable.Id_menu_badge_icon).setWidth(AttrHelper.vp2px(20, MainAbilitySlice.this));
            c01.findComponentById(ResourceTable.Id_menu_badge_icon).setHeight(AttrHelper.vp2px(20, MainAbilitySlice.this));
            IconicsImageView iconicsImageView = (IconicsImageView) c01.findComponentById(ResourceTable.Id_menu_badge_icon);
            Text text = (Text) c01.findComponentById(ResourceTable.Id_menu_badge);
            text.setMarginLeft(AttrHelper.vp2px(0, MainAbilitySlice.this));
            iconicsImageView.setMarginLeft(AttrHelper.vp2px(0, MainAbilitySlice.this));
            if (robot_drawable != null) {
                robot_drawable.setColor(ohos.agp.utils.Color.WHITE);
                iconicsImageView.setIcon(robot_drawable);
            }
            c01.setLayoutConfig(layoutConfig2);
            c02.setLayoutConfig(layoutConfig5);
            c03.setLayoutConfig(layoutConfig2);
            c04.setLayoutConfig(layoutConfig5);
            component01.setLayoutConfig(layoutConfig2);
            c05.setLayoutConfig(layoutConfig3);

            Button button = (Button) c05.findComponentById(ResourceTable.Id_menu_badge);
            toolbar_dependentLayout.addComponent(ActionItemBadge_Id);
            toolbar_dependentLayout.addComponent(component01);
            toolbar_dependentLayout.addComponent(c05);

        }

    }

    @Override
    protected void onBackPressed() {

        if (isCheckActivity == true) {

            if (isCheckActivity == true && isCheckHide == true) {
                changeLayout();
            }
            isCheckActivity = false;
            badgeDrawableCount = recordbadgeDrawableCount;
            ActionItemBadge_Id.setVisibility(Component.VISIBLE);
            ActionItemBadge_Id.setLeft(AttrHelper.vp2px(80, MainAbilitySlice.this));
            dl_toptitle.setVisibility(Component.VISIBLE);
            handle.setWidth(AttrHelper.vp2px(30, MainAbilitySlice.this));
            badgeCount = beforeM;
            changePosition(beforeP);
        } else {
            super.onBackPressed();
            RecordStatus.setHideCheck(false);
            RecordStatus.setSwitchAbility(false);
            terminateAbility();
        }
    }

    private void adapteList() {
        ListAdapter listAdapter = new ListAdapter(MainAbilitySlice.this, beanContentList);
        listContainer_right.setItemProvider(listAdapter);
        listContainer_right.setLongClickable(false);
        ListLeftAdapter leftAdapter = new ListLeftAdapter(MainAbilitySlice.this, leftList);
        leftAdapter.setmItemOnClickListener(this::itemOnClickListener);
        list_main.setItemProvider(leftAdapter);
    }

    private void initDate() {
        beanContentList = new ArrayList<>();
        leftList = new ArrayList<>();
        leftList.add(new LeftBean("Default (Dark Grey)", false));
        leftList.add(new LeftBean("Grey", false));
        leftList.add(new LeftBean("Red", true));
        leftList.add(new LeftBean("Blue", false));
        leftList.add(new LeftBean("Green", false));
        leftList.add(new LeftBean("Purple", false));
        leftList.add(new LeftBean("Yellow", false));
        leftList.add(new LeftBean("Custom", false));
        ListBeanContent l0 = new ListBeanContent("Aboutlibraries", "Mike Penz",
            "AboutLibraries " +
            "is a library to offer you all the information you need of your libraries!" + NEWLINE +
            "Most modern apps feature a \"Used Library\"-section, which requires information" +
                " about those " + "respective libs. As it gets annoying to always copy those " +
                "strings to your app, I have " +
            "developed this small helper library to provide the required information.",
            "7.0.3", "Apache Version 2.0",
            getString(ResourceTable.String_url_10));
        ListBeanContent l1 = new ListBeanContent("Aboutlibraries", "Mike Penz",
            "AboutLibraries " +
            "is a library to offer you all the information you need of your libraries! "+ NEWLINE +
            "Most modern apps feature a \"Used Library\"-section, which requires information " +
                "about those " + "respective libs. As it gets annoying to always copy those " +
                "strings to your app," + " I have " + "developed this small helper library to" +
                " provide the required information.", "7.0.3", "Apache Version 2.0",
            getString(ResourceTable.String_url_11));
        ListBeanContent l2 = new ListBeanContent("Ohos-ActionItemBadge", "Mike Penz",
            "Ohos-ActionItemBadge " + "is a library which offers a simple and easy to " +
                "use method to add a badge to your action item!!", "3.3.2", "Apache " +
            "Version 2.0", getString(ResourceTable.String_url_12));
        ListBeanContent l3 = new ListBeanContent("Ohos-Iconics", "Mike Penz",
            "This library allows you to include " + "vector icons everywhere in your project. " +
                "No limits are given. Scale with no limit, use any Color at any time, provide a contour," +
            " and many additional customizations", "4.0.1-b02", "Apache Version 2.0",
            getString(ResourceTable.String_url_13));
        ListBeanContent l4 = new ListBeanContent("Constraint Layout", "AOSP",
            "The ConstraintLayout package allows " +
            "you to create large and complex layouts with a flat view hierarchy", "1.0.2",
            "Apache Version 2.0", getString(ResourceTable.String_url_14));
        ListBeanContent l5 = new ListBeanContent("Crossfader", "Mike Penz",
            "Crossfader", "1.6.0", "Apache Version 2.0",
            getString(ResourceTable.String_url_15));
        ListBeanContent l6 = new ListBeanContent("FastAdapter", "Mike Penz",
            "FastAdapter, the bullet proof, fast and easy to use adapter" +
            " library , which minimizes developing time to a fraction... ", "4.1.0",
            "Apache Version 2.0", getString(ResourceTable.String_url_16));
        ListBeanContent l7 = new ListBeanContent("Font Awesome", "Dave Gandy",
            "Font Awesome gives you scalable vector icons that can " +
            "instantly be customized - size, color, drop shadow, and anything that can be done with the power of " +
                "CSS.", "5.9.0.0-kotlin", "SIL Open Font License 1.1",
            getString(ResourceTable.String_url_17));
        ListBeanContent l8 = new ListBeanContent("Google Material Design Ic...", "Google Inc",
            "Material design icons are" + " the offical icon set from Google that are designed " +
                "under the material design guidelines", "3.0.1.4.original-kotlin",
            "Creative Commons 4.0", getString(ResourceTable.String_url_18));
        ListBeanContent l9 = new ListBeanContent("IntelliJ IDEA Annotations", "Jetbrain developers",
            "A set of " + "annotations used for code inspection support and code documentation.",
            "12.0.0", "Apache Version 2.0", getString(ResourceTable.String_url_19));
        ListBeanContent l10 = new ListBeanContent("MaterialDrawer", "Mike Penz",
            "MaterialDrawer the fiexible , " +
            "easy to use , all in one drawer library for your Ohos project.", "7.0.0-rc08",
            "Apache Version 2.0", getString(ResourceTable.String_url_20));
        ListBeanContent l11 = new ListBeanContent("Materialize", "Mike Penz", "Materialize " +
            "comes with usefull " + "helper classes, the completes material color palette, and many additional " +
            "utils. Use its theme as base. Let it manage " + "your StatusBar, NavigationBar, FullScreen " +
            "behavior...", "1.2.1", "Apache Version 2.0", getString(ResourceTable.String_url_21));
        ListBeanContent l12 = new ListBeanContent("MultiDex", "AOSP", "MultiDex patches" +
            " the application context" +
            " class loader in order to load classes from more than one dex file.", "1.0.0",
            "Apache Version 2.0",
            getString(ResourceTable.String_url_22));
        ListBeanContent l13 = new ListBeanContent("Support Annotations", "AOSP", "The " +
            "Annotation package provides" + " APIs to support adding annotation metadata to your apps.",
            "1.0.0", "Apache Version 2.0",
            getString(ResourceTable.String_url_23));
        beanContentList.add(l0);
        beanContentList.add(l1);
        beanContentList.add(l2);
        beanContentList.add(l3);
        beanContentList.add(l4);
        beanContentList.add(l5);
        beanContentList.add(l6);
        beanContentList.add(l7);
        beanContentList.add(l8);
        beanContentList.add(l9);
        beanContentList.add(l10);
        beanContentList.add(l11);
        beanContentList.add(l12);
        beanContentList.add(l13);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void itemOnClickListener(int position) {
        if (leftList != null && leftList.size() > 0) {
            isNormal = true;
            slidingDrawer.animateClose();
            if (isCheckHide == true) {
                RecordStatus.setHideCheck(true);
            } else {
                RecordStatus.setHideCheck(false);
            }
            changePosition(position);
        }
    }

    private Timer timer = new Timer();
    private TimerTask timerTask = new TimerTask() {
        public void run() {
            if (popupWindowDialog != null)
                popupWindowDialog.destroy();
        }
    };

    @Override
    public boolean onOptionsItemLongSelected(Component component) {
        int id = component.getId();
        if (id == ResourceTable.Id_item_samplebadge) {
            RgbColor rgbColor = new RgbColor(220, 220, 220);
            popWindowShow("Sample 1");
        } else if (id == ResourceTable.Id_item_sampleBadge_drawable) {
            popWindowShow("Sample 1");
        } else if (id == ResourceTable.Id_item_sampleBadge_large) {
            popWindowShow("Sample 1");
        } else if (id == ResourceTable.Id_item_sampleBadge_large2) {
            popWindowShow("Sample 1");
        } else {
            popWindowShow("Sample 2");
        }
        return false;
    }

    private void popWindowShow(String str) {
        popupWindowDialog = new PopupWindowDialog(this, componentline, str);
        popupWindowDialog.setSize(250, 150);
        popupWindowDialog.setAutoClosable(true);
        popupWindowDialog.setTransparent(true);
        popupWindowDialog.setBackColor(new Color(Color.getIntColor("#00000000")));
        popupWindowDialog.show();
        task = new Runnable() {
            @Override
            public void run() {
                // 待执行的操作，由开发者定义
                popupWindowDialog.destroy();
            }
        };
        mEventHandler.postTask(task, 2000, EventHandler.Priority.IMMEDIATE); // 延时250ms后立即处理
    }
}
